package com.xiebishe.mylibrary.common.glide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.xiebishe.mylibrary.common.ResUtils;


public class ImageLoadRequestBuilder {


    private Context context;


    private UrlRequestOption urlRequestOption;

    private Boolean autoFitUrl=false;
    private Boolean autoFitUrlCutting=false;
    private Boolean autoFitUrlSize=false;

    private boolean isCircle;

    private Drawable placeHolder;
    private Drawable errorHolder;
    private boolean isCenterCrop;

    private ImageView imageView;
    private int viewWidth;
    private int viewHeight;


    public ImageLoadRequestBuilder(Context context) {
        this.context = context;
        urlRequestOption = new UrlRequestOption();

    }

    public ImageLoadRequestBuilder url(String url) {
        urlRequestOption.setUrl(url);
        return this;
    }

    public ImageLoadRequestBuilder needCuttingManually() {
        urlRequestOption.setNeedCutting(true);
        return this;
    }

    public ImageLoadRequestBuilder urlWidthHeightManually(int urlWidth, int urlHeight) {
        urlRequestOption.setSourceWidth(urlWidth);
        urlRequestOption.setSourceHeight(urlHeight);
        return this;
    }

    /**
     * 根据view的大小自动设置相关的url参数。
     *
     * [注意:如果调用此方法，就不要有任何主动改变原URL的行为!!!!!!!!!!如：
     * {@link #needCuttingManually()}
     * {@link #urlWidthHeightManually(int, int)} ()}
     * {@link ImageLoader#parseImageUrl(String, int, int, boolean)}]
     *
     * @param autoFitCutting
     * @param autoFitSize
     * @return
     */
    public ImageLoadRequestBuilder autoFitUrl(boolean autoFitCutting,boolean autoFitSize){
        this.autoFitUrlCutting=autoFitCutting;
        this.autoFitUrlSize=autoFitSize;
        autoFitUrl=autoFitUrlCutting||autoFitUrlSize;
        return this;
    }



    public ImageLoadRequestBuilder circle() {
        this.isCircle = true;
        return this;
    }

    public ImageLoadRequestBuilder centerCrop() {
        this.isCenterCrop = true;
        return this;
    }

    public ImageLoadRequestBuilder placeHolder(Drawable placeHolder) {
        this.placeHolder = placeHolder;
        return this;
    }

    public ImageLoadRequestBuilder placeHolder(@DrawableRes int drawableId) {
        if (drawableId != 0) {
            this.placeHolder = ResUtils.getDrawable(drawableId);
        }
        return this;
    }

    public ImageLoadRequestBuilder errorHolder(Drawable errorHolder) {
        this.errorHolder = placeHolder;
        return this;
    }

    public ImageLoadRequestBuilder errorHolder(@DrawableRes int drawableId) {
        if (drawableId != 0) {
            this.errorHolder = ResUtils.getDrawable(drawableId);
        }
        return this;
    }

    public ImageLoadRequestBuilder imageView(ImageView imageView) {
        this.imageView = imageView;
        return this;
    }

    public ImageLoadRequestBuilder viewWidthHeight(int viewWidth, int viewHeight) {
        this.viewWidth = viewWidth;
        this.viewHeight = viewHeight;
        return this;
    }

    public String load() {
        //处理url
        if(autoFitUrl){
            urlRequestOption.setNeedCutting(false);
            urlRequestOption.setSourceWidth(0);
            urlRequestOption.setSourceHeight(0);
        }
        String finalUrl = urlRequestOption.build();
        //判断svg
        boolean isSvg = finalUrl != null && (finalUrl.contains(".svg") || finalUrl.contains("/svg/"));
        ImageLoader.loadImage(context, finalUrl, isSvg, isCircle, isCenterCrop, autoFitUrlCutting,autoFitUrlSize,placeHolder, errorHolder, imageView, viewWidth, viewHeight);
        return finalUrl;
    }
}
